class Button extends Sprite{
  /*
  点击按钮升级炮筒时，按下的按钮和默认按钮是不同的图片，
  所以需要传入默认按钮的图片和按下按钮的图片
  */
  constructor(default_image_data,down_image_data){
    super({
      image:  default_image_data.image,
      sx:     default_image_data.frame.x,
      sy:     default_image_data.frame.y,
      w:      default_image_data.frame.w,
      h:      default_image_data.frame.h
    });
    this.defaultImageData = default_image_data;
    this.downImageData = down_image_data;
  }

  __check(x,y){
    if (
      this.x-this.w/2 <= x && this.x+this.w/2 >= x &&
      this.y-this.h/2 <= y && this.y+this.h/2 >= y
    ) {
      return true;
    } else {
      return false;
    }
  }

  /*
  检测是否按下按钮
  入参x,y是鼠标在canvas的坐标位置
  */
  checkDown(x,y){
    if (this.__check(x,y)) {
      this.image = this.downImageData.image;
      this.sx = this.downImageData.frame.x;
      this.sy = this.downImageData.frame.y;
      this.w = this.downImageData.frame.w;
      this.h = this.downImageData.frame.h;
      return true;
    } else {
      return false;
    }
  }

  /*
  检测是否抬起按钮
  */
  checkUp(x,y){
    if (this.__check(x,y)) {
      this.image = this.defaultImageData.image;
      this.sx = this.defaultImageData.frame.x;
      this.sy = this.defaultImageData.frame.y;
      this.w = this.defaultImageData.frame.w;
      this.h = this.defaultImageData.frame.h;
      return true;
    } else {
      return false;
    }
  }
}
